package com.yfbao.horizon.schedule.tk.meter;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.binder.MeterBinder;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

@Component
public class TestMetrics implements MeterBinder {

    private static Timer threadTimer =null;
    private static Timer threadTimer2 =null;

    @Override
    public void bindTo(MeterRegistry registry) {
        threadTimer =  Timer.builder("ctp_thread_duration_time")
                .description("async ctp-thread execution time")
                .tags("status", "success") // 可选：添加标签
//                .publishPercentileHistogram(true) // 发布百分位直方图
                .serviceLevelObjectives(
                        Duration.ofMillis(500),  //小于等0.5s定义桶的范围
                        Duration.ofMillis(2000),  //小于等2s定义桶的范围
                        Duration.ofMillis(30000)  //小于等30s定义桶的范围
                )
                .register(registry);
        threadTimer2 =  Timer.builder("ctp_thread_duration_time")
                .description("async ctp-thread execution time2")
                .tags("status", "success") // 可选：添加标签
//                .publishPercentileHistogram(true) // 发布百分位直方图
                .serviceLevelObjectives(
                        Duration.ofMillis(500),  //小于等0.5s定义桶的范围
                        Duration.ofMillis(2000),  //小于等2s定义桶的范围
                        Duration.ofMillis(30000)  //小于等30s定义桶的范围
                )
                .register(registry);
    }

    public static void setThreadTimer(Long costTIme){
        if(threadTimer ==null){
            return;
        }
        try {
            threadTimer.record(costTIme, TimeUnit.MILLISECONDS);
        } catch (Exception e) {}
    }
}
